package com.cy.ppj.comm.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.text.SimpleDateFormat;

/**
 * @description: ObjectMapper
 * @author: Ocean
 * @create: 2018-06-14 11:22
 **/
@Configuration
public class ObjectMapperConfig {

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper()
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) // 字段可见
                .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE) // get方法不可见
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)// 跳过未知字段
                .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)// 跳过空值字段
                .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return objectMapper;
    }

}
